// js/JobData.js
import { getJobData } from './axios.js';


const initializeJobData = async () => {
    try {
        // 获取总数据
        let JobData = await getJobData();
        const Datas = JobData.length;   // 数据总长度

        // 项目类型
        let projectTypeList = {};
        JobData.forEach(item => {
            if (projectTypeList[item.projectType]) {
                projectTypeList[item.projectType]++
            } else {
                projectTypeList[item.projectType] = 1
            }
        })
        projectTypeList = Object.entries(projectTypeList).map(([name, value]) => ({ name, value }));
        projectTypeList = projectTypeList.sort((a, b) => b.value - a.value)
        if (projectTypeList.length > 5) {
            projectTypeList = projectTypeList.slice(0, 5)
        }
        var projectTypeListName = []
        var projectTypeListValue = []
        projectTypeList.forEach(item => {
            projectTypeListName.push(item.name)
            projectTypeListValue.push(item.value)
        })

        // 地区分布部分
        let householdList = {} // 地区分布数据
        JobData.forEach(item => {
            if (householdList[item.address]) {
                householdList[item.address]++
            } else {
                householdList[item.address] = 1
            }
        });
        // 将对象转换成数组
        householdList = Object.entries(householdList).map(([name, value]) => ({ name, value }));
        // 按照value排序
        householdList = householdList.sort((a, b) => b.value - a.value)
        if (householdList.length > 8) {
            householdList = householdList.slice(0, 8)
        }


        // 主流技术部分
        let technology = {} // 技术分布数据 
        JobData.forEach(item => {
            if (technology[item.technology]) {
                technology[item.technology]++
            } else {
                technology[item.technology] = 1
            }
        })
        // 将对象转换成数组
        technology = Object.entries(technology).map(([name, value]) => ({ name, value }));
        // 按照value排序
        technology = technology.sort((a, b) => b.value - a.value)
        if (technology.length > 5) {
            technology[0].value = 100
            technology[1].value = 80
            technology[2].value = 60
            technology[3].value = 40
            technology[4].value = 20
            technology = technology.slice(0, 5)
        }


        // 岗位分布
        let positionList = []
        let positionObj = {}
        JobData.forEach(item => {
            if (positionObj[item.position]) {
                positionObj[item.position]++
            } else {
                positionObj[item.position] = 1
            }
        })
        positionObj = Object.entries(positionObj).map(([name, value]) => ({ name, value }));
        positionObj = positionObj.sort((a, b) => b.value - a.value)
        positionObj = positionObj.slice(0, 4)
        let positionListNameOneList = [positionObj[0].name]
        let positionListNameTwoList = [positionObj[1].name]
        let positionListNameThreeList = [positionObj[2].name]
        let positionListNameFourList = [positionObj[3].name]
        var positionListNameOneNum7 = 0;
        var positionListNameOneNum8 = 0;
        var positionListNameOneNum9 = 0;
        var positionListNameOneNum10 = 0;
        var positionListNameTwoNum7 = 0;
        var positionListNameTwoNum8 = 0;
        var positionListNameTwoNum9 = 0;
        var positionListNameTwoNum10 = 0;
        var positionListNameThreeNum7 = 0;
        var positionListNameThreeNum8 = 0;
        var positionListNameThreeNum9 = 0;
        var positionListNameThreeNum10 = 0;
        var positionListNameFourNum7 = 0;
        var positionListNameFourNum8 = 0;
        var positionListNameFourNum9 = 0;
        var positionListNameFourNum10 = 0;
        JobData.forEach(item => {
            if (item.position === positionListNameOneList[0] && item.month === 7) {
                // 计算数量
                positionListNameOneNum7 += 1;
            } else if (item.position === positionListNameOneList[0] && item.month === 8) {
                positionListNameOneNum8 += 1;
            } else if (item.position === positionListNameOneList[0] && item.month === 9) {
                positionListNameOneNum9 += 1;
            } else if (item.position === positionListNameOneList[0] && item.month === 10) {
                positionListNameOneNum10 += 1;
            } else if (item.position === positionListNameTwoList[0] && item.month === 7) {
                positionListNameTwoNum7 += 1;
            } else if (item.position === positionListNameTwoList[0] && item.month === 8) {
                positionListNameTwoNum8 += 1;
            } else if (item.position === positionListNameTwoList[0] && item.month === 9) {
                positionListNameTwoNum9 += 1;
            } else if (item.position === positionListNameTwoList[0] && item.month === 10) {
                positionListNameTwoNum10 += 1;
            } else if (item.position === positionListNameThreeList[0] && item.month === 7) {
                positionListNameThreeNum7 += 1;
            } else if (item.position === positionListNameThreeList[0] && item.month === 8) {
                positionListNameThreeNum8 += 1;
            } else if (item.position === positionListNameThreeList[0] && item.month === 9) {
                positionListNameThreeNum9 += 1;
            } else if (item.position === positionListNameThreeList[0] && item.month === 10) {
                positionListNameThreeNum10 += 1;
            } else if (item.position === positionListNameFourList[0] && item.month === 7) {
                positionListNameFourNum7 += 1;
            } else if (item.position === positionListNameFourList[0] && item.month === 8) {
                positionListNameFourNum8 += 1;
            } else if (item.position === positionListNameFourList[0] && item.month === 9) {
                positionListNameFourNum9 += 1;
            } else if (item.position === positionListNameFourList[0] && item.month === 10) {
                positionListNameFourNum10 += 1;
            }
        })
        positionListNameOneList.push(positionListNameOneNum7)
        positionListNameOneList.push(positionListNameOneNum8)
        positionListNameOneList.push(positionListNameOneNum9)
        positionListNameOneList.push(positionListNameOneNum10)
        positionListNameTwoList.push(positionListNameTwoNum7)
        positionListNameTwoList.push(positionListNameTwoNum8)
        positionListNameTwoList.push(positionListNameTwoNum9)
        positionListNameTwoList.push(positionListNameTwoNum10)
        positionListNameThreeList.push(positionListNameThreeNum7)
        positionListNameThreeList.push(positionListNameThreeNum8)
        positionListNameThreeList.push(positionListNameThreeNum9)
        positionListNameThreeList.push(positionListNameThreeNum10)
        positionListNameFourList.push(positionListNameFourNum7)
        positionListNameFourList.push(positionListNameFourNum8)
        positionListNameFourList.push(positionListNameFourNum9)
        positionListNameFourList.push(positionListNameFourNum10)

        positionList.push(['月份', '7', '8', '9', '10'])
        positionList.push(positionListNameOneList)
        positionList.push(positionListNameTwoList)
        positionList.push(positionListNameThreeList)
        positionList.push(positionListNameFourList)


        // 平均薪资
        let averageSalaryList = []
        let maxSalary = 0;
        let sumSalary = 0;
        JobData.forEach(item => {
            averageSalaryList.push(item.salary)
            sumSalary += item.salary
        })
        sumSalary = (sumSalary / averageSalaryList.length).toFixed(0)
        const maxSalaryLi = document.getElementById('maxSalaryLi')
        const avgSalaryLi = document.getElementById('avgSalaryLi')
        averageSalaryList.forEach(item => {
            if (item > maxSalary) {
                maxSalary = item
            }
        });
        maxSalaryLi.innerText = maxSalary
        avgSalaryLi.innerText = sumSalary

        // 处理地图数据
        let addressList = {};
        let addressLists = [];
        JobData.forEach(item => {
            if (addressList[item.household]) {
                addressList[item.household] += 1
            } else {
                addressList[item.household] = 1
            }
        })
        addressList = Object.entries(addressList).map(([name, peopleNum]) => ({ name, peopleNum }));
        addressList = addressList.sort((a, b) => b.peopleNum - a.peopleNum)

        if (addressList.length > 8) {
            addressList = addressList.slice(0, 8)
        }

        addressList.forEach(item => {
            item.value = 100;
        })
        addressList.forEach(item => {
            addressLists.push([{ name: "保定" }, item])
        })



        return {
            householdList, // 户籍分布
            technology,  //  主流技术
            projectTypeListName,  // 项目类型名称
            projectTypeListValue, // 项目类型数量
            positionList, // 职位分布
            addressLists, //  就业地区分布
        };
    } catch (error) {
        console.error("Error initializing JobData:", error);
    }
};

// 初始化 JobData
initializeJobData();

export { initializeJobData }